package net.begincode.servlet.down;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import utils.BQStringUtil;
import utils.ViewCount;

import com.google.gson.Gson;

import net.begincode.bean.BcDown;
import net.begincode.bean.BcUser;
import net.begincode.servlet.common.BaseServlet;
import net.begincode.servlet.common.ViewServlet;

public class DownServlet extends BaseServlet{
	private static final Log LOG = LogFactory.getLog(DownServlet.class);
	
	/**
	 * 新增下载初始化
	 * */
	public void addDownInit(HttpServletRequest request, HttpServletResponse response){
		 this.selDownNav(request);
		try {
			
			request.getRequestDispatcher("/WEB-INF/manage/editDown.jsp").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 保存下载
	 * */
	public void saveDown(HttpServletRequest request, HttpServletResponse response){
		BcDown bcDown = new BcDown();
		//初始化表单信息
		bcDown.setBc_navigation_id(Integer.valueOf(request.getParameter("navId")));
		bcDown.setDown_name(request.getParameter("downName"));
		bcDown.setDown_intro(request.getParameter("downIntro"));
		bcDown.setKey_word(request.getParameter("downKeyWord"));
		bcDown.setDown_addr(request.getParameter("downAdd"));
		bcDown.setDown_abstract(request.getParameter("downAbstract"));
		//初始化其他信息
		bcDown.setDown_count(0);
		bcDown.setCheck_flag("1");
		String bcDownId = request.getParameter("bcDownId");
		int returnValue = 0;
		if(bcDownId.equals("")){
			returnValue = this.bcDownService.addBcDown(bcDown);
		}else{
			bcDown.setBc_down_id(Integer.parseInt(bcDownId));
			returnValue  = this.bcDownService.updDown(bcDown);
		}
		if(returnValue > 0){
			request.setAttribute("msg", "保存成功");
		}else{
			request.setAttribute("bcDown", bcDown);
			request.setAttribute("msg", "保存失败");
		}
		try {
			this.selDownNav(request);
			request.getRequestDispatcher("/WEB-INF/manage/editDown.jsp").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 查看资源列表
	 * @throws IOException 
	 * @throws ServletException 
	 * */
	public void downList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		String navId = (String)request.getAttribute("id");
		request.setAttribute("downs", this.bcDownService.selDownByNavIdCache(navId));
		request.setAttribute("hotdowns", this.bcDownService.selHotDownLimitCache(this.BOOK, 10));
		request.setAttribute("hodCodes", this.codeService.selHotCodeLimitCache(10));
		request.setAttribute("navInfo",this.managerService.selNavByIdCache(navId));
		request.getRequestDispatcher("/WEB-INF/page/down_list.jsp").forward(request, response);
	}
	
	/**
	 * 查看下载
	 * @throws IOException 
	 * @throws ServletException 
	 * */
	public void viewDown(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		String downId = (String)request.getAttribute("id");
		ViewCount.addViewCount(ViewServlet.VIEW_DOWN+":"+downId); //添加阅读数
		BcDown downInfo = this.bcDownService.selDownByIdCache(downId);
		request.setAttribute("downInfo",downInfo );
		request.setAttribute("hotdowns", this.bcDownService.selDownByNavIdCache(String.valueOf(downInfo.getBc_navigation_id())));
		request.setAttribute("navInfo", this.managerService.selNavByIdCache(String.valueOf(downInfo.getBc_navigation_id())));
		request.getRequestDispatcher("/WEB-INF/page/down_view.jsp").forward(request, response);
	}
 
	/**
	 * 查询下载信息
	 * @throws IOException 
	 * @throws ServletException 
	 * */
	public void selDownByParam(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		request.setAttribute("bcDowns", this.bcDownService.selDownByParam(new BcDown()));
		request.getRequestDispatcher("/WEB-INF/manage/downManage.jsp").forward(request, response);
		
	}
	
	/**
	 * 修改初始化
	 * @throws IOException 
	 * @throws ServletException 
	 * */
	public void updDownInit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		String bcDownId = request.getParameter("bcDownId");
		request.setAttribute("bcDown", this.bcDownService.selDownById(bcDownId));
		this.selDownNav(request);
		request.getRequestDispatcher("/WEB-INF/manage/editDown.jsp").forward(request, response);
	}
}
